<?php

class ArtistController extends Zend_Controller_Action {

    public function init() {
        $this->view->headTitle('MusicEvents')->headTitle('Artist')->setSeparator(' :: ');
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('search', 'html')
                ->addActionContext('info', 'html')
                ->addActionContext('similar', 'html')
                ->addActionContext('album', 'html')
                ->addActionContext('events', 'json')
                ->initContext();
    }

    public function validateAction() {
        if ($this->getRequest()->isPost()) {
            $form = new Application_Form_Artist();
            $this->view->accordion = 0;
            if ($this->getRequest()->isPost()) {
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)) {
                    $this->getRequest()->setDispatched();
                    $filter = new Zend_Filter_StripTags();
                    $this->_redirect('artist/search/' . urlencode($filter->filter($form->getValue('artist'))));
                } else {
                    $form->populate($formData);
                    $this->view->artistForm = $form;
                }
            }
        }
    }

    public function searchAction() {
        if ($this->getRequest()->has('artist')) {
            $artist = $this->_getParam('artist');

            if (isset($artist)) {
                $page = $this->_getParam('page', 1);
                $itemCountPerPage = 8;
                $paginator = new Zend_Paginator(new Application_Model_Artist($artist, $itemCountPerPage, $page));
                $paginator->setItemCountPerPage($itemCountPerPage);
                $paginator->setCurrentPageNumber($page);
                $this->view->searchPattern = $artist;
                $this->view->matches = $paginator->getTotalItemCount();
                $this->view->pages = ceil($paginator->getTotalItemCount() / $paginator->getItemCountPerPage());
                $this->view->paginator = $paginator;
                $this->view->headTitle('Search for '. $artist);
                $form = new Application_Form_Artist();
                $form->populate(array('artist' => $artist));
                $this->view->artistForm = $form;
            }
        }
    }

    public function showAction() {
        if ($this->getRequest()->has('artist')) {
            $artist = urlencode($this->_getParam('artist'));
            Lastfm_Artist::isValid(urldecode($artist));
            $this->view->artist = urldecode($artist);
            $this->view->info_ajax = '/artist/info/artist/' . $artist;
            $this->view->similar_ajax = '/artist/similar/artist/' . $artist;
            $this->view->album_ajax = '/artist/album/artist/' . $artist;
            $this->view->event_json = '/artist/events/artist/' . $artist;
            $this->view->event_tmp = '/template/artist';
            $this->view->headTitle(urldecode($artist));
            $response = $this->getResponse();
            $response->insert('sidecontent', $this->view->render('artist/side.phtml'));
        }
    }

    public function infoAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $artist = $this->_getParam('artist');
            $this->view->artistInfo = Lastfm_Artist::getInfo($artist, $mbid = null, $lang = 'en');
        }
    }

    public function similarAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $artist = $this->_getParam('artist');
            $this->view->artistSimilar = Lastfm_Artist::getSimilar($artist, 3);
        }
    }

    public function albumAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $artist = $this->_getParam('artist');
            $this->view->artistTopAlbums = Lastfm_Artist::getTopAlbums($artist, 3);
        }
    }

    public function eventsAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $artist = $this->_getParam('artist');
            $page = $this->_getParam('page', 1);
            $limit = $this->_getParam('limit', 1);
            $events = Lastfm_Artist::getEvents($artist, $limit, $page);

            echo json_encode(Application_Model_Event::toArray($events));
            exit;
        }
    }

}

